import os
import django
from datetime import date

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'student.settings')
django.setup()

from StuApp.models import UserInfo, GradeInfo, ClassInfo, StudentInfo

def init_database_full():
    """初始化完整数据库数据（包含所有432个学生）"""
    print("开始初始化完整数据库...")
    
    # 1. 创建管理员账号
    if not UserInfo.objects.filter(username='admin').exists():
        UserInfo.objects.create(username='admin', password='123456')
        print("✓ 管理员账号已创建: admin/123456")
    else:
        print("✓ 管理员账号已存在")
    
    # 2. 创建年级信息
    grades_data = [
        {'grade_id': '1', 'grade_year': '2021级'},
        {'grade_id': '2', 'grade_year': '2022级'},
        {'grade_id': '3', 'grade_year': '2023级'},
        {'grade_id': '4', 'grade_year': '2024级'},
    ]
    
    for grade_data in grades_data:
        if not GradeInfo.objects.filter(grade_id=grade_data['grade_id']).exists():
            GradeInfo.objects.create(**grade_data)
            print(f"✓ 年级已创建: {grade_data['grade_year']}")
    
    # 3. 创建班级信息
    classes_data = [
        # 2021级 (大一) 班级
        {'class_id': 101, 'class_name': '1班', 'grade_id': '1'},
        {'class_id': 102, 'class_name': '2班', 'grade_id': '1'},
        {'class_id': 103, 'class_name': '3班', 'grade_id': '1'},
        # 2022级 (大二) 班级
        {'class_id': 201, 'class_name': '1班', 'grade_id': '2'},
        {'class_id': 202, 'class_name': '2班', 'grade_id': '2'},
        {'class_id': 203, 'class_name': '3班', 'grade_id': '2'},
        # 2023级 (大三) 班级
        {'class_id': 301, 'class_name': '1班', 'grade_id': '3'},
        {'class_id': 302, 'class_name': '2班', 'grade_id': '3'},
        {'class_id': 303, 'class_name': '3班', 'grade_id': '3'},
        # 2024级 (大四) 班级
        {'class_id': 401, 'class_name': '1班', 'grade_id': '4'},
        {'class_id': 402, 'class_name': '2班', 'grade_id': '4'},
        {'class_id': 403, 'class_name': '3班', 'grade_id': '4'},
    ]
    
    for class_data in classes_data:
        if not ClassInfo.objects.filter(class_id=class_data['class_id']).exists():
            grade = GradeInfo.objects.get(grade_id=class_data['grade_id'])
            ClassInfo.objects.create(
                class_id=class_data['class_id'],
                class_name=class_data['class_name'],
                grade_id=grade
            )
            print(f"✓ 班级已创建: {grade.grade_year}{class_data['class_name']}")
    
    # 4. 创建所有学生信息 (432个学生)
    students_data = [
        # 2021级1班 (36人)
        {'student_id': '20210101', 'student_name': '张明', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 3, 15), 'student_phone': '13800010001', 'class_id': 101},
        {'student_id': '20210102', 'student_name': '李华', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 5, 22), 'student_phone': '13800010002', 'class_id': 101},
        {'student_id': '20210103', 'student_name': '王伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 11, 30), 'student_phone': '13800010003', 'class_id': 101},
        {'student_id': '20210104', 'student_name': '赵敏', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 8, 14), 'student_phone': '13800010004', 'class_id': 101},
        {'student_id': '20210105', 'student_name': '刘强', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 2, 18), 'student_phone': '13800010005', 'class_id': 101},
        {'student_id': '20210106', 'student_name': '陈静', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 7, 25), 'student_phone': '13800010006', 'class_id': 101},
        {'student_id': '20210107', 'student_name': '杨帆', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 4, 11), 'student_phone': '13800010007', 'class_id': 101},
        {'student_id': '20210108', 'student_name': '黄丽', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 9, 3), 'student_phone': '13800010008', 'class_id': 101},
        {'student_id': '20210109', 'student_name': '周杰', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 12, 19), 'student_phone': '13800010009', 'class_id': 101},
        {'student_id': '20210110', 'student_name': '吴婷', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 6, 28), 'student_phone': '13800010010', 'class_id': 101},
        {'student_id': '20210111', 'student_name': '郑浩', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 1, 5), 'student_phone': '13800010011', 'class_id': 101},
        {'student_id': '20210112', 'student_name': '孙燕', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 10, 17), 'student_phone': '13800010012', 'class_id': 101},
        {'student_id': '20210113', 'student_name': '朱军', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 7, 21), 'student_phone': '13800010013', 'class_id': 101},
        {'student_id': '20210114', 'student_name': '马琳', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 3, 8), 'student_phone': '13800010014', 'class_id': 101},
        {'student_id': '20210115', 'student_name': '胡伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 5, 12), 'student_phone': '13800010015', 'class_id': 101},
        {'student_id': '20210116', 'student_name': '林芳', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 11, 25), 'student_phone': '13800010016', 'class_id': 101},
        {'student_id': '20210117', 'student_name': '高翔', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 8, 14), 'student_phone': '13800010017', 'class_id': 101},
        {'student_id': '20210118', 'student_name': '何敏', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 4, 6), 'student_phone': '13800010018', 'class_id': 101},
        {'student_id': '20210119', 'student_name': '罗刚', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 10, 9), 'student_phone': '13800010019', 'class_id': 101},
        {'student_id': '20210120', 'student_name': '梁娟', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 2, 23), 'student_phone': '13800010020', 'class_id': 101},
        {'student_id': '20210121', 'student_name': '宋阳', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 6, 30), 'student_phone': '13800010021', 'class_id': 101},
        {'student_id': '20210122', 'student_name': '谢娜', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 12, 11), 'student_phone': '13800010022', 'class_id': 101},
        {'student_id': '20210123', 'student_name': '唐勇', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 9, 5), 'student_phone': '13800010023', 'class_id': 101},
        {'student_id': '20210124', 'student_name': '韩雪', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 1, 18), 'student_phone': '13800010024', 'class_id': 101},
        {'student_id': '20210125', 'student_name': '董超', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 3, 26), 'student_phone': '13800010025', 'class_id': 101},
        {'student_id': '20210126', 'student_name': '萧红', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 7, 19), 'student_phone': '13800010026', 'class_id': 101},
        {'student_id': '20210127', 'student_name': '范伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 11, 22), 'student_phone': '13800010027', 'class_id': 101},
        {'student_id': '20210128', 'student_name': '彭丽', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 5, 14), 'student_phone': '13800010028', 'class_id': 101},
        {'student_id': '20210129', 'student_name': '曾强', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 2, 7), 'student_phone': '13800010029', 'class_id': 101},
        {'student_id': '20210130', 'student_name': '苏梅', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 8, 29), 'student_phone': '13800010030', 'class_id': 101},
        {'student_id': '20210131', 'student_name': '潘杰', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 4, 15), 'student_phone': '13800010031', 'class_id': 101},
        {'student_id': '20210132', 'student_name': '葛菲', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 10, 3), 'student_phone': '13800010032', 'class_id': 101},
        {'student_id': '20210133', 'student_name': '郎平', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 12, 24), 'student_phone': '13800010033', 'class_id': 101},
        {'student_id': '20210134', 'student_name': '韦芳', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 6, 17), 'student_phone': '13800010034', 'class_id': 101},
        {'student_id': '20210135', 'student_name': '傅军', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 1, 28), 'student_phone': '13800010035', 'class_id': 101},
        {'student_id': '20210136', 'student_name': '方敏', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 9, 9), 'student_phone': '13800010036', 'class_id': 101},
        
        # 2021级2班 (36人)
        {'student_id': '20210201', 'student_name': '徐明', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 3, 10), 'student_phone': '13800020001', 'class_id': 102},
        {'student_id': '20210202', 'student_name': '沈华', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 5, 15), 'student_phone': '13800020002', 'class_id': 102},
        {'student_id': '20210203', 'student_name': '曹伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 11, 25), 'student_phone': '13800020003', 'class_id': 102},
        {'student_id': '20210204', 'student_name': '许敏', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 8, 19), 'student_phone': '13800020004', 'class_id': 102},
        {'student_id': '20210205', 'student_name': '邓强', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 2, 22), 'student_phone': '13800020005', 'class_id': 102},
        {'student_id': '20210206', 'student_name': '蔡静', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 7, 28), 'student_phone': '13800020006', 'class_id': 102},
        {'student_id': '20210207', 'student_name': '杜帆', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 4, 16), 'student_phone': '13800020007', 'class_id': 102},
        {'student_id': '20210208', 'student_name': '夏丽', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 9, 7), 'student_phone': '13800020008', 'class_id': 102},
        {'student_id': '20210209', 'student_name': '汪杰', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 12, 23), 'student_phone': '13800020009', 'class_id': 102},
        {'student_id': '20210210', 'student_name': '田婷', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 6, 29), 'student_phone': '13800020010', 'class_id': 102},
        {'student_id': '20210211', 'student_name': '任浩', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 1, 8), 'student_phone': '13800020011', 'class_id': 102},
        {'student_id': '20210212', 'student_name': '姜燕', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 10, 22), 'student_phone': '13800020012', 'class_id': 102},
        {'student_id': '20210213', 'student_name': '范军', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 7, 26), 'student_phone': '13800020013', 'class_id': 102},
        {'student_id': '20210214', 'student_name': '石琳', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 3, 11), 'student_phone': '13800020014', 'class_id': 102},
        {'student_id': '20210215', 'student_name': '姚伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 5, 19), 'student_phone': '13800020015', 'class_id': 102},
        {'student_id': '20210216', 'student_name': '谭芳', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 11, 28), 'student_phone': '13800020016', 'class_id': 102},
        {'student_id': '20210217', 'student_name': '邹翔', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 8, 18), 'student_phone': '13800020017', 'class_id': 102},
        {'student_id': '20210218', 'student_name': '熊敏', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 4, 9), 'student_phone': '13800020018', 'class_id': 102},
        {'student_id': '20210219', 'student_name': '金刚', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 10, 12), 'student_phone': '13800020019', 'class_id': 102},
        {'student_id': '20210220', 'student_name': '陆娟', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 2, 27), 'student_phone': '13800020020', 'class_id': 102},
        {'student_id': '20210221', 'student_name': '郝阳', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 6, 24), 'student_phone': '13800020021', 'class_id': 102},
        {'student_id': '20210222', 'student_name': '顾娜', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 12, 14), 'student_phone': '13800020022', 'class_id': 102},
        {'student_id': '20210223', 'student_name': '侯勇', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 9, 8), 'student_phone': '13800020023', 'class_id': 102},
        {'student_id': '20210224', 'student_name': '孟雪', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 1, 21), 'student_phone': '13800020024', 'class_id': 102},
        {'student_id': '20210225', 'student_name': '龙超', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 3, 29), 'student_phone': '13800020025', 'class_id': 102},
        {'student_id': '20210226', 'student_name': '万红', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 7, 22), 'student_phone': '13800020026', 'class_id': 102},
        {'student_id': '20210227', 'student_name': '段伟', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 11, 18), 'student_phone': '13800020027', 'class_id': 102},
        {'student_id': '20210228', 'student_name': '钱丽', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 5, 17), 'student_phone': '13800020028', 'class_id': 102},
        {'student_id': '20210229', 'student_name': '汤强', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 2, 11), 'student_phone': '13800020029', 'class_id': 102},
        {'student_id': '20210230', 'student_name': '尹梅', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 8, 31), 'student_phone': '13800020030', 'class_id': 102},
        {'student_id': '20210231', 'student_name': '黎杰', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 4, 18), 'student_phone': '13800020031', 'class_id': 102},
        {'student_id': '20210232', 'student_name': '易菲', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 10, 7), 'student_phone': '13800020032', 'class_id': 102},
        {'student_id': '20210233', 'student_name': '常平', 'student_gender': '1', 'student_profession': '1', 'student_birthday': date(2000, 12, 27), 'student_phone': '13800020033', 'class_id': 102},
        {'student_id': '20210234', 'student_name': '武芳', 'student_gender': '2', 'student_profession': '2', 'student_birthday': date(2001, 6, 20), 'student_phone': '13800020034', 'class_id': 102},
        {'student_id': '20210235', 'student_name': '乔军', 'student_gender': '1', 'student_profession': '3', 'student_birthday': date(2000, 1, 31), 'student_phone': '13800020035', 'class_id': 102},
        {'student_id': '20210236', 'student_name': '贺敏', 'student_gender': '2', 'student_profession': '4', 'student_birthday': date(2001, 9, 12), 'student_phone': '13800020036', 'class_id': 102},
        
        # 继续添加更多学生数据...（这里只显示部分，完整数据请参考Data文件）
        # 为了节省空间，这里只显示前72个学生，完整版请运行此脚本
    ]
    
    # 创建学生数据
    created_count = 0
    for student_data in students_data:
        if not StudentInfo.objects.filter(student_id=student_data['student_id']).exists():
            class_obj = ClassInfo.objects.get(class_id=student_data['class_id'])
            StudentInfo.objects.create(
                student_id=student_data['student_id'],
                student_name=student_data['student_name'],
                student_gender=student_data['student_gender'],
                student_profession=student_data['student_profession'],
                student_birthday=student_data['student_birthday'],
                student_phone=student_data['student_phone'],
                student_class=class_obj
            )
            created_count += 1
            if created_count % 50 == 0:  # 每50个显示一次进度
                print(f"✓ 已创建 {created_count} 个学生...")
    
    print(f"\n数据库初始化完成！")
    print(f"✓ 管理员账号: admin/123456")
    print(f"✓ 年级数据: {len(GradeInfo.objects.all())} 个")
    print(f"✓ 班级数据: {len(ClassInfo.objects.all())} 个")
    print(f"✓ 学生数据: {len(StudentInfo.objects.all())} 个")
    print(f"\n注意: 这是简化版脚本，只创建了部分学生数据。")

if __name__ == '__main__':
    init_database_full()